﻿
using Microsoft.EntityFrameworkCore;
using FreeRedis;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc;
using static System.Reflection.Metadata.BlobBuilder;
using Newtonsoft.Json.Linq;
using CQIE.LabAssist.Models;
using System;
using CQIE.LabAssist.PublicHelp;
using System.Drawing;
using Microsoft.Extensions.Caching.Distributed;
namespace CQIE.LabAssist.DataAccesss
{

    public class FloorService : IFloorService
    {

        private readonly _DbContext _Db;
        private readonly JwtHelp _jwtHelper;

        private IDistributedCache RedisCache;

        public FloorService(JwtHelp jwtHelper, _DbContext db, IDistributedCache redisCache)
        {
            _jwtHelper = jwtHelper;
            _Db = db;
            RedisCache = redisCache;
        }

        /// <summary>
        /// 根据楼栋id获取所有楼层
        /// </summary>
        /// <param name="buildingId"></param>
        /// <returns></returns>
        public JsonResult getAllFloor(int buildingId)
        {
            string? RedisResult = RedisCache.GetString("AllFloors");
            if (RedisResult == null)
            {
                var AllFloor = _Db.Floors.Where(w => w.buildingId == buildingId).ToList();

                // 将结果转换为JSON字符串
                string jsonString = JsonConvert.SerializeObject(AllFloor);

                var options = new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10),
                };
                RedisCache.SetString("AllFloors", jsonString, options);

                return new JsonResult(AllFloor);
            }
            else
            {
                var cachedData = RedisCache.GetString("AllFloors");
                var AllFloors = JsonConvert.DeserializeObject<List<Floor>>(cachedData);
                // 返回JsonResult
                return new JsonResult(AllFloors);
            }
        }

        /// <summary>
        /// 新增楼层
        /// </summary>
        /// <param name="floor"></param>
        /// <returns></returns>
        public bool  addFloor(Floor floor)
        {
            Floor f = new Floor();
            f.Name= floor.Name;
            f.buildingId= floor.buildingId;
            f.building=_Db.Buildings.Where(w=>w.Id==floor.buildingId).FirstOrDefault();
            _Db.Floors.Add(f);
            return _Db.SaveChanges()>0;
        }
        /// <summary>
        /// 删除楼层
        /// </summary>
        /// <param name="floorId"></param>
        /// <returns></returns>
        public bool deteltFloor(int floorId)
        {
            Floor Floors = _Db.Floors.Where(w => w.Id == floorId).FirstOrDefault();
            _Db.Floors.Remove(Floors);
            return _Db.SaveChanges() > 0;
        }
        /// <summary>
        /// 更新楼层
        /// </summary>
        /// <param name="floor"></param>
        /// <returns></returns>
        public bool updateFloor(Floor floor)
        {
            Floor f = _Db.Floors.Where(w=>w.Id==floor.Id).FirstOrDefault();
            f.Name=floor.Name;
  
            _Db.Floors.Update(f);
            return _Db.SaveChanges() > 0;
        }
    }

    
}
